var dtree;
var layer;
var $;
var table;
var element;
var util;
var form;
var laydate;
layui.extend({
    dtree: '/dTree/layui_ext/dtree/dtree'
}).use(['element','layer','util', 'dtree','jquery', 'form','laydate'], function(){
    dtree = layui.dtree;
    layer = layui.layer;
    $ = layui.jquery;
    table = layui.table;
    element = layui.element;
    util = layui.util;
    form = layui.form;
    laydate = layui.laydate;

    //树
    getInformationTree();
    //列表
    getInformation();

    //调用权限
    getRole();

});
function getInformation(){
    var createUser = $("input[name=createUser]").val();
    var uId =  localStorage.getItem("creatId");
    console.log("新一层的名字是"+createUser);
    $.ajax({
        url:"/articletype/getArticletype",
        type:"post",
        dataType:"json",
        data:{"createUser":uId},
        success:function(data){
            //hidden
            $("input[name=createUser]").val(data.allName);
            $("input[name=deptId]").val(data.deptId);
            $("input[name=positionId]").val(data.positionId);
            $("input[name=gradeId]").val(data.gradeId);
            $("input[name=diplomaId]").val(data.diplomaId);
            $("input[name=secrecyLevel]").val(data.secrecyLevel);
            //select
            $("input[name=sex]").val(data.sex);
            $("input[name=positionName]").val(data.positionName);
            $("input[name=status]").val(data.status);
            $("input[name=gradeName]").val(data.gradeName);
            $("input[name=political]").val(data.political);
            $("input[name=diplomaName]").val(data.diplomaName);
            $("input[name=allName]").val(data.allName);
            $("input[name=deptName]").val(data.deptName);
            $("input[name=secrecyLevelName]").val(data.secrecyLevelName);
            $("input[name=qualifications]").val(data.qualifications);
            $("input[name=tel1]").val(data.tel1);
            $("input[name=tel2]").val(data.tel2);
            $("input[name=email]").val(data.email);
            $("input[name=fax]").val(data.fax);
            $("input[name=specialty]").val(data.specialty);
            $("input[name=idCard]").val(data.idCard);
            $("input[name=graduationDate]").val(data.graduationDate);
            $("input[name=workDate]").val(data.workDate);
            $("input[name=entryDate]").val(data.entryDate);
            $("input[name=turnoverDate]").val(data.turnoverDate);
            $("input[name=regdate]").val(data.regdate);
            $("input[name=lastDate]").val(data.lastDate);
            $("input[name=lastLocalHost]").val(data.lastLocalHost);


            var role = data.roleName;
            for(var a=0;a<data.length;a++){
                var roleName = data.roleName[a];
                var roleId = data.roleId[a];
                $(".span_3").append("<span class='span_rol'><span id='roleName'>"+roleName+"</span><span class='span_4'><a href=\"javascript:delRole('"+roleName+"',"+roleId+")\"><img src='/images/icon19.gif'></a></span></span>");
            }

            //鑾峰彇select鍊�
            var sex = $("input[name=sex]").val();//鎬у埆data.sex
            var status = $("input[name=status]").val();//鐘舵€乨ata.status
            var political = $("input[name=political]").val();//鏀挎不闈㈣矊data.political
            var positionId = $("input[name=positionId]").val();//鑱屽姟id
            var gradeId = $("input[name=gradeId]").val();//寰呴亣id
            var diplomaId = $("input[name=diplomaId]").val();//瀛﹀巻
            //璁剧疆select榛樿閫夊€�
            $("#sex").val(sex);
            $("#positionName").val(positionId);
            $("#status").val(status);
            $("#gradeName").val(gradeId);
            $("#political").val(political);
            $("#diplomaName").val(diplomaId);
            form.render('select','selFilter');
        }
    })
}
function getRole(){
    var roleId =  localStorage.getItem("roleId");
    var uId =  localStorage.getItem("roleName");
    $.ajax({
        url:"/articletype/getCreateInformations",
        type:"post",
        dataType:"json",
        data:{"uId":uId},
        success:function(data) {
            var role = data.roleName;
            for (var a = 0; a < data.length; a++) {
                var roleName = data[a].roleName;
                var roleId = data[a].roleId;
                $(".span_3").append("<span class='span_rol'><span id='roleName'>" + roleName + "</span><span class='span_4'><a href=\"javascript:delRole('" + roleName + "'," + roleId + ")\"><img src='/images/icon19.gif'></a></span></span>");
            }
        }
    })
}
function delRole(name,id){
    var uId =  localStorage.getItem("roleName");
    name=uId;
    console.log(name + id);
    var uId = $("input[name=createUser]").val();
    $.ajax({
        url:"/articletype/delRole",
        type:"post",
        dataType:"json",
        data:{"uId":name,"rId":id},
        success:function(data){
            if(data){
                $(".span_rol").remove();
                getRole();
            }
        }
    })
}
//后台获取树后绑定
function getInformationTree(){
    var CreateTree = dtree.render({
        elem: "#test1",
        url: "../../../json/case/GetUser.json",
        dataFormat: "list",  //配置data的风格为list
        method:"get"
    })
}
//查看部门
function findDept(){
    layer.open({
        type: 2,
        title:"部门信息",
        area: ['700px', '500px'],
        shade: 0,
        offset: 'auto',
        content: "/layer-Dept.html",
        success:function(){

        }
    })
}
//添加权限页面
function addRoleName() {
    layer.open({
        type: 2,
        title: "添加权限",
        area: ['700px', '500px'],
        shade: 0,
        offset: 'auto',
        content: "/layer-addRoleName.html",
        btn: ['确定','取消'],
        yes: function (layero, index) {
            console.log("************************");
            var name = localStorage.getItem("roleName");
            var roleId =  localStorage.getItem("roleId");
            var uId = $("input[name=createUser]").val();

            addRole(roleId,name)
            layer.close(layer.index);
            layer.msg("添加成功");
        },btn2:function(){
            layer.close(layer.index);
        }
    })
}
function addRole(roleId,name){
    var uId =  localStorage.getItem("creatId");
    console.log("要给管理员"+uId+"添加的权限是"+roleId);
    if(roleId!=0&&name!=0){
        $.ajax({
            url:"/articletype/addRole",
            type:"post",
            dataType:"json",
            data:{"uId":uId,"rId":roleId},
            success:function(data){
                if(data){
                    console.log("页面添加结果是"+data);
                    $(".span_rol").remove();
                    getRole();
                }
            }
        })
    }
}
//鐐瑰嚮缂栬緫  缂栬瘧鐢ㄦ埛淇℃伅
function editInformation(){
    $(".td_input").removeAttr("templates.error.style");
    $(".td_input").removeAttr("disabled");
    $(".td_select_layui").removeAttr("disabled");
    $("#div_btn").val("完成");
    $("#div_btn").removeAttr("onclick");
    $("#div_btn").attr("onclick","saveInformation()");
    $(".td_image").attr("templates.error.style",'display:inline-block');
    $(".span_2").attr("templates.error.style",'display:inline-block');

    laydate.render({
        elem: '#graduationDate', //鎸囧畾鍏冪礌
        type: 'datetime',
        trigger: 'click' //鏃堕棿鏃堕棿涓虹偣鍑讳簨浠�
    });
    laydate.render({
        elem: '#workDate', //鎸囧畾鍏冪礌
        type: 'datetime',
        trigger: 'click' //鏃堕棿鏃堕棿涓虹偣鍑讳簨浠�
    });
    laydate.render({
        elem: '#entryDate', //鎸囧畾鍏冪礌
        type: 'datetime',
        trigger: 'click' //鏃堕棿鏃堕棿涓虹偣鍑讳簨浠�
    });
    laydate.render({
        elem: '#turnoverDate', //鎸囧畾鍏冪礌
        type: 'datetime',
        trigger: 'click' //鏃堕棿鏃堕棿涓虹偣鍑讳簨浠�
    });
    //鑾峰彇select鍊�
    var sex = $("input[name=sex]").val();//鎬у埆data.sex
    var status = $("input[name=status]").val();//鐘舵€乨ata.status
    var political = $("input[name=political]").val();//鏀挎不闈㈣矊data.political
    var positionId = $("input[name=positionId]").val();//鑱屽姟id
    var gradeId = $("input[name=gradeId]").val();//寰呴亣id
    var diplomaId = $("input[name=diplomaId]").val();//瀛﹀巻
    //璁剧疆select榛樿閫夊€�
    $("#sex").val(sex);
    $("#positionName").val(positionId);
    $("#status").val(status);
    $("#gradeName").val(gradeId);
    $("#political").val(political);
    $("#diplomaName").val(diplomaId);
    form.render('select','selFilter');
}
//鐐瑰嚮淇濆瓨锛岀紪杈戞垚鍔�
function saveInformation(){
    $(".td_input").attr("disabled","disabled");
    $(".td_select_layui").attr("disabled","disabled");
    $(".layui-unselect").attr("disabled","disabled");
    form.render('select');
    $("#div_btn").val("编辑");
    $("#div_btn").removeAttr("onclick");
    $("#div_btn").attr("onclick","editInformation()");
    $(".td_image").attr("templates.error.style",'display:none');
    var sex =  $("#sex").val();
    var positionId = $("#positionName").val();
    var status = $("#status").val();
    var gradeId = $("#gradeName").val();
    var political = $("#political").val();
    var diplomaId = $("#diplomaName").val();
    $("input[name=sex]").val(sex);
    $("input[name=status]").val(status);
    $("input[name=political]").val(political);
    $("input[name=positionId]").val(positionId);
    $("input[name=gradeId]").val(gradeId);
    $("input[name=diplomaId]").val(diplomaId);
    var secrecyLevelName= $("input[name=secrecyLevelName]").val();
    var qualifications= $("input[name=qualifications]").val();
    var tel1= $("input[name=tel1]").val();
    var tel2= $("input[name=tel2]").val();
    var email= $("input[name=email]").val();
    var fax =$("input[name=fax]").val();
    var specialty =$("input[name=specialty]").val();
    var idCard =$("input[name=idCard]").val();
    var graduationDate =$("input[name=graduationDate]").val();
    var workDate =$("input[name=workDate]").val();
    var entryDate =$("input[name=entryDate]").val();
    var turnoverDate =$("input[name=turnoverDate]").val();
    var regdate =$("input[name=regdate]").val();
    var lastDate =$("input[name=lastDate]").val();
    var regip =$("input[name=regip]").val();
    var allName =$("input[name=allName]").val();
    var deptName = $("input[name=deptName]").val();
    var createUser = $("input[name=createUser]").val();
    var deptId =  $("input[name=deptId]").val();//閮ㄩ棬id
    var secrecyLevel = $("input[name=secrecyLevel]").val();//淇濆瘑绾у埆id
    var id =  localStorage.getItem("creatId");
    console.log("作为参数传入后台的id是"+id);
    $.ajax({
        url:"/articletype/updateCreateInformation",
        type:"post",
        dataType:"json",
        data:{
            "allName":allName,"secrecyLevelName":secrecyLevelName,"qualifications":qualifications,"tel1":tel1,"tel2":tel2,"email":email,
            "fax":fax,"specialty":specialty,"idCard":idCard,"graduationDate":graduationDate,"workDate":workDate,"entryDate":entryDate,
            "turnoverDate":turnoverDate,"regdate":regdate,"lastDate":lastDate,"regip":regip,"deptName":deptName,
            "diplomaId":diplomaId,"gradeId":gradeId,"positionId":positionId,"political":political,"status":status,"sex":sex,
            "id":id,"deptId":deptId,"secrecyLevel":secrecyLevel
        },
        success:function(data){
            console.log(data);
            if(data){
                layer.msg("修改成功");
            }
        }
    })
}